package com.classeven.downhill.object;




import java.util.Iterator;

import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.classeven.downhill.define.iDefine_Object;
import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.Anchor;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class Mc implements IAccelerationListener/*SensorEventListener*/ {

	public ASprite Mc;
//	boolean sensor;
	boolean move;
	
	private int state =0;
	private float scale =1;
	private byte sc;

	SensorManager sensorManager;
	Sensor accelerometer;	
	
	float sX, sY;
	
	public Mc(GameEngine engine)
	{
		Mc = new ASprite(engine, iGameDefines.SPRITE_GAME, 1);
		Mc.theSprite().setY(GameEngine.CAMERA_HEIGHT / 4);
		engine.getEngine().enableAccelerationSensor(engine, this);

//		 sensorManager = (SensorManager) engine.getSystemService(engine.SENSOR_SERVICE);
//	        accelerometer = sensorManager
//	                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//	
//	        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
	    	
	}
	

	
	public void onUpdate()
	{
		switch (state) 
		{
		case iDefine_Object.MC_LEFT_RIGHT: // left right 
			
			break;

		case iDefine_Object.MC_JUMP:		//jump
			
			switch (sc) {
			case 0:
				Mc.theSprite().setScale(scale += 0.025);
				
				if(Mc.theSprite().getScaleX() >= 2)
					sc = 1;
				break;

			case 1:
				Mc.theSprite().setScale(scale -= 0.025);
				
				if(Mc.theSprite().getScaleX() <= 1)
				{
					state = iDefine_Object.MC_LEFT_RIGHT;
					sc=0;
				}
				
				break;
			}
			
			Utils.TRACE("jummmmmmmmmmppppppppppppppppppp");
			break;
		}
			
		
		
	}
	
	public ASprite getMc()
	{
		return Mc;
	}
	
	public void setMove(boolean mov)
	{
		move = mov;
	}

	public void setState(int state)
	{
		this.state = state;
	}



	@Override
	public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAccelerationChanged(AccelerationData pAccelerationData) {
		// TODO Auto-generated method stub

		float ac = -4 + pAccelerationData.getX();
		float acc = ac % -4;
		switch (state) 
		{
		case 0:  
			
		if(move)
		{
			if(pAccelerationData.getX() <= 0 && pAccelerationData.getX() >= -1)
				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER));
			else if (pAccelerationData.getX() >= 0 && pAccelerationData.getX() <= 0.1)
				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER));
			else
			{
				
				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER) + (GameEngine.CAMERA_WIDTH / 4 )+ (pAccelerationData.getX() * 10 ));
				
			}
		}
	
//		Utils.TRACE("Accurary = "+pAccelerationData.getAccuracy());
	
		Utils.TRACE("X = "+pAccelerationData.getX());
		 
			break;
			
			
		}
	}



//	@Override
//	public void onAccuracyChanged(Sensor arg0, int arg1) {
//		// TODO Auto-generated method stub
//		
//	}
//
//
//
//	@Override
//	public void onSensorChanged(SensorEvent arg0) {
//		switch (state) 
//		{
//		case 0:  
//			
//		if(move)
//		{
//			if(arg0.values[0] <= 0 && arg0.values[0] >= -0.1 )
//				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER));
//			else if (arg0.values[0] >= 0 && arg0.values[0] <= 0.1)
//				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER));
//			else
//				Mc.theSprite().setX(Utils.GetAnchorX(Mc.theSprite().getWidth(), Anchor.CENTER) + (arg0.values[0] * 10 ));
//		}	
//	
////		Utils.TRACE("Accurary = "+pAccelerationData.getAccuracy());
//	
//		Utils.TRACE("X = "+arg0.values[0]);
//		
//	
//			break;
//
//			
//		}
//		
//	}

	
	
}
